package repository

import (
	"github.com/gccai/write_helper/model"
	"github.com/jinzhu/gorm"
)

type DesignRepository struct {
	DB *gorm.DB
}

type DesignRepositoryInterface interface {
	// 获取 卡片的具体内容 -->DId
	Get(card model.Design) (*model.Design, error)
	// 获取章节或者书的所有卡片--> 1.userId,bookName,designAttribution.
	//2.userId, bookName, chapterId,designAttribution
	//3. userId, bookName,--->获取本书所有的卡片。
	// todo 更改所有的[]*model.xxx --> []model.xxx
	GetAllCard(userId, bookName string) (*[]model.Design, error)
	GetOneTypeCard(userId, bookName string, da int64) (*[]model.Design, error)
	//GetChapterCard(userId, bookName, chapterId string, da int64) (*[]model.Design, error)
	// 增加一个卡片
	Add(card model.Design) (bool, error)
	// 编辑一个卡片的内容
	Edit(card model.Design) (*model.Design, error)
	// 删除一个card
	Delete(userId, bookName, dId string) (bool, error)
	// 检查是否有这个card，确保的是传输过程，数据不会出现问题。
	Exist(userId, bookName, dId string) bool
}

func (d DesignRepository) Get(card model.Design) (*model.Design, error) {
	var res model.Design
	err := d.DB.Where("user_id = ? and book_name = ? and d_id = ? and is_deleted = ?",
		card.UserId, card.BookName, card.DId, false).Find(&res).Error
	if err != nil {
		return nil, err
	}
	return &res, nil
}

func (d DesignRepository) GetAllCard(userId, bookName string) (*[]model.Design, error) {
	var cards []model.Design
	err := d.DB.Where("user_id = ? and book_name = ? and is_deleted = ?",
		userId, bookName, false).Find(&cards).Error
	if err != nil {
		return nil, err
	}
	return &cards, nil
}

func (d DesignRepository) GetOneTypeCard(userId, bookName string, da int64) (*[]model.Design, error) {
	var cards []model.Design
	err := d.DB.Where("user_id = ? and book_name = ? and design_attribution = ? and is_deleted = ?",
		userId, bookName, da, false).Find(&cards).Error
	if err != nil {
		return nil, err
	}
	return &cards, nil
}

func (d DesignRepository) Add(card model.Design) (bool, error) {
	err := d.DB.Create(&card).Error
	if err != nil {
		return false, err
	}
	return true, nil
}

func (d DesignRepository) Edit(card model.Design) (*model.Design, error) {
	exist, err := d.Get(card)
	if err != nil {
		return nil, err
	}
	err = d.DB.Model(exist).Where("user_id = ? and book_name = ? and d_id = ? and is_deleted = ?",
		card.UserId, card.BookName, card.DId, false).Updates(map[string]interface{}{
		"design_title":   card.DesignTitle,
		"design_content": card.DesignContent,
	}).Error
	if err != nil {
		return nil, err
	}
	return exist, nil
}

func (d DesignRepository) Delete(userId, bookName, dId string) (bool, error) {
	var card model.Design
	err := d.DB.Model(&card).Where("user_id = ? and book_name = ? and d_id = ?",
		userId, bookName, dId).Update(map[string]interface{}{
		"is_deleted": true,
	}).Error
	if err != nil {
		return false, err
	}
	return true, nil
}

func (d DesignRepository) Exist(userId, bookName, dId string) bool {
	var card model.Design
	err := d.DB.Where("user_id = ? and book_name = ? and d_id = ? and is_deleted = ?",
		userId, bookName, dId, false).Find(&card).Error
	if err != nil {
		return false
	}
	return true
}
